﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chaine.Models;
using MongoDB.Bson;
using System.IO;
using System.Drawing;

namespace Chaine.DAO
{
    public static class PhotoManager
    {
        public static string GetEventPhotoPath(this EventModels e)
        {
            return HttpContext.Current.Server.MapPath(string.Format("/events/photo/{0}/photos/", e.StartDateTime.ToString("yyyyMMdd")));
        }

        public static string GetEventThumbsPhotoPath(this EventModels e)
        {
            return HttpContext.Current.Server.MapPath(string.Format("/events/photo/{0}/thumbs/", e.StartDateTime.ToString("yyyyMMdd")));
        }

        public static bool DeletePhoto(ObjectId eventID, string photoName)
        {
            var e = EventManager.GetEvent(eventID);
            if (e == null)
                return false;
            var fileName = e.GetEventPhotoPath() + photoName;
            var thumbFileName = e.GetEventThumbsPhotoPath() + photoName;
            //删除大图片
            if (File.Exists(fileName))
            {
                try
                {
                    File.Delete(fileName);
                }
                catch (Exception)
                {
                    throw;
                    //return false;
                }
            }
            //删除缩略图
            if (File.Exists(thumbFileName))
            {
                try
                {
                    File.Delete(thumbFileName);
                }
                catch (Exception)
                {
                    throw;
                    //return false;
                }
            }
            return !File.Exists(fileName) && !File.Exists(thumbFileName);
        }

        public static bool UploadPhotos(ObjectId eventId, HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
                return false;
            var e = EventManager.GetEvent(eventId);
            var fileName = Guid.NewGuid().ToString() + ".jpg";
            var photoDir = e.GetEventPhotoPath();
            if (!Directory.Exists(photoDir))
                Directory.CreateDirectory(photoDir);
            var photoPath = Path.Combine(photoDir, fileName);

            var thumbDir = e.GetEventThumbsPhotoPath();
            if (!Directory.Exists(thumbDir))
                Directory.CreateDirectory(thumbDir);
            var thumbPath = Path.Combine(thumbDir, fileName);
            //保存图片
            file.SaveAs(photoPath);
            file.InputStream.Close();
            file.InputStream.Dispose();

            using (var img = Image.FromFile(photoPath))
            {
                //定死高度
                var height = 69;
                //自适应宽度
                var width = height * img.Width / img.Height;

                //生成缩略图
                YueEasy.Tool.Image.ResizeImage(photoPath, thumbPath, width, height, width, height);
            }
            return true;
        }
    }
}